module.exports = function(window, document) {
	var apivm = require('../../common/avm.min')(window, document);
	var avm = apivm;
	var api = apivm.api;
	var Component = apivm.Component;
	var $ = apivm.$;
	(function() {
	/*
	 * APICloud JavaScript Library
	 * Copyright (c) 2014 apicloud.com
	 */
	var $kn = {
		byId: function byId(id) {
			return document.getElementById(id);
		},
		ajax: function ajax(p, callback) {
			var _this = this;
			var param = p;
			if (!param.headers) {
				param.headers = {};
			}
			param.headers["x-apicloud-mcm-key"] = "cZKzX7DabDmYyfez";
			if (param.data && param.data.body) {
				param.headers["Content-Type"] = "application/json; charset=utf-8";
			}
			if (param.url) {
				var baseUrl = "https://a8888888888888-pd.apicloud-saas.com/api/";
				param.url = baseUrl + param.url;
			}
			api.ajax(param, function(ret, err) {
				if (callback) callback(ret, err);
				if (ret) {
					var status = ret.status;
					if (status && status == 4001) {
						var didShowLogoutAlert = api.getGlobalData({
							key: "didShowLogoutAlert"
						});

						if (!didShowLogoutAlert) {
							api.setGlobalData({
								key: "didShowLogoutAlert",
								value: true
							});

							_this.setUserInfo("");
							api.alert(
								{
									msg: "登录已失效，请重新登录"
								},
								function() {
									api.setGlobalData({
										key: "didShowLogoutAlert",
										value: false
									});

									api.closeToWin({
										name: "root"
									});
								}
							);
						}
					}
				}
			});
		},
		getUserInfo: function getUserInfo() {
			var value = api.getPrefs({
				key: "userInfo",
				sync: true
			});

			return value ? JSON.parse(value) : "";
		},
		setUserInfo: function setUserInfo(userInfo) {
			api.setPrefs({
				key: "userInfo",
				value: userInfo
			});
		},
		getCurrentCityInfo: function getCurrentCityInfo() {
			var value = api.getPrefs({
				key: "currentCity",
				sync: true
			});

			return value ? JSON.parse(value) : "";
		},
		setCurrentCityInfo: function setCurrentCityInfo(cityInfo) {
			api.setPrefs({
				key: "currentCity",
				value: cityInfo
			});
		},
		getWareTypeList: function getWareTypeList() {
			var value = api.readFile({
				sync: true,
				path: "fs://WareTypeList"
			});

			return value ? JSON.parse(value) : "";
		},
		setWareTypeList: function setWareTypeList(list) {
			api.writeFile({
				path: "fs://WareTypeList",
				data: JSON.stringify(list)
			});
		},
		fitRichText: function fitRichText(richtext, width) {
			var str = '<img style="max-width:' + width + 'px;"';
			var result = richtext.replace(/\<img/gi, str);
			return result;
		}
	};

	var Sharepage = /*@__PURE__*/ (function(Component) {
		function Sharepage(props) {
			Component.call(this, props);
			this.data = {};
		}

		if (Component) Sharepage.__proto__ = Component;
		Sharepage.prototype = Object.create(Component && Component.prototype);
		Sharepage.prototype.constructor = Sharepage;
		Sharepage.prototype.render = function() {
			return apivm.h(
				"view",
				{class: "sharepage-main"},
				apivm.h("view", {class: "sharepage-section", onclick: this.props.onresult}),
				apivm.h(
					"view",
					{class: "sharepage-footer"},
					apivm.h(
						"view",
						{class: "sharepage-col", onclick: this.props.onresult},
						apivm.h("image", {
							class: "sharepage-img",
							src: "../../image/share_timeline.png"
						}),
						apivm.h("text", null, "微信朋友圈")
					),
					apivm.h(
						"view",
						{class: "sharepage-col", onclick: this.props.onresult},
						apivm.h("image", {
							class: "sharepage-img",
							src: "../../image/share_friend.png"
						}),
						apivm.h("text", null, "微信好友")
					)
				)
			);
		};

		return Sharepage;
	})(Component);
	apivm.define("sharepage", Sharepage);

	var Minicart = /*@__PURE__*/ (function(Component) {
		function Minicart(props) {
			Component.call(this, props);
			this.data = {
				amount: 0,
				count: 0
			};
		}

		if (Component) Minicart.__proto__ = Component;
		Minicart.prototype = Object.create(Component && Component.prototype);
		Minicart.prototype.constructor = Minicart;
		Minicart.prototype.installed = function() {
			this.initEventListenner();
			if ($kn.getUserInfo()) {
				this.getCartInfo();
			}
		};
		Minicart.prototype.initEventListenner = function() {
			var that = this;
			// 监听onGetCartInfo事件（自定义），更新商品数量和总金额
			api.addEventListener(
				{
					name: "onGetCartInfo"
				},
				function(ret, err) {
					var data = ret.value;
					that.data.amount = Math.floor(data.totalPrice * 10) / 10;
					that.data.count = data.totalCount;
				}
			);
			api.addEventListener(
				{
					name: "onLogin"
				},
				function(ret, err) {
					that.getCartInfo();
				}
			);
			api.addEventListener(
				{
					name: "onLogout"
				},
				function(ret, err) {
					that.data.amount = 0;
					that.data.count = 0;
				}
			);
		};
		Minicart.prototype.fnOpenShoppingCartWin = function() {
			if (!$kn.getUserInfo()) {
				api.openWin({
					name: "login",
					url: "../login/login.stml"
				});

				return;
			}
			api.openWin({
				name: "shoppingcart",
				url: "../shoppingcart/shoppingcart.stml"
			});
		};
		Minicart.prototype.getCartInfo = function() {
			$kn.ajax(
				{
					url: "carts/getCartInfo"
				},
				function(ret, err) {
					if (ret && ret.data) {
						api.setGlobalData({key: "cartInfo", value: ret.data});
						api.sendEvent({
							name: "onGetCartInfo",
							extra: ret.data
						});
					}
				}
			);
		};
		Minicart.prototype.render = function() {
			return apivm.h(
				"view",
				{class: "__main", onclick: this.fnOpenShoppingCartWin},
				apivm.h("text", {class: "__amount"}, "￥" + this.data.amount),
				apivm.h(
					"text",
					{class: this.data.count > 0 ? "__count" : "__count __count-none"},
					this.data.count
				)
			);
		};

		return Minicart;
	})(Component);
	apivm.define("minicart", Minicart);

	var StatusBarView = /*@__PURE__*/ (function(Component) {
		function StatusBarView(props) {
			Component.call(this, props);
			this.compute = {
				statusBarHeight: function() {
					if (api.safeArea) {
						return api.safeArea.top;
					} else {
						var res = wx.getSystemInfoSync();
						return res.statusBarHeight;
					}
				}
			};
		}

		if (Component) StatusBarView.__proto__ = Component;
		StatusBarView.prototype = Object.create(Component && Component.prototype);
		StatusBarView.prototype.constructor = StatusBarView;
		StatusBarView.prototype.render = function() {
			return apivm.h("view", {style: "height:" + this.statusBarHeight + "px;"});
		};

		return StatusBarView;
	})(Component);
	apivm.define("status-bar-view", StatusBarView);

	var Navigationbar = /*@__PURE__*/ (function(Component) {
		function Navigationbar(props) {
			Component.call(this, props);
		}

		if (Component) Navigationbar.__proto__ = Component;
		Navigationbar.prototype = Object.create(Component && Component.prototype);
		Navigationbar.prototype.constructor = Navigationbar;
		Navigationbar.prototype.onLeftButton = function(e) {
			api.closeWin();
		};
		Navigationbar.prototype.render = function() {
			return apivm.h(
				"view",
				{class: "nav-container"},
				apivm.h("status-bar-view", null),
				apivm.h(
					"view",
					{class: "nav-header"},
					apivm.h(
						"view",
						{
							class: "nav-header-button nav-left-button",
							onclick: this.props.onLeftButton
								? this.props.onLeftButton
								: this.onLeftButton
						},
						apivm.h("image", {
							width: this.props.leftButtonWidth ? this.props.leftButtonWidth : 11,
							src: this.props.leftButtonIcon
								? this.props.leftButtonIcon
								: "../../image/back.png",
							mode: "widthFix"
						}),
						apivm.h("text", {class: "nav-header-text"}, this.props.leftButtonText)
					),
					apivm.h("text", {class: "nav-header-title"}, this.props.title),
					apivm.h(
						"view",
						{
							class: "nav-header-button nav-right-button",
							onclick: this.props.onRightButton
						},
						apivm.h("image", {
							width: this.props.rightButtonWidth ? this.props.rightButtonWidth : 0,
							src: this.props.rightButtonIcon ? this.props.rightButtonIcon : "",
							mode: "widthFix"
						}),
						apivm.h("text", {class: "nav-header-text"}, this.props.rightButtonText)
					)
				)
			);
		};

		return Navigationbar;
	})(Component);
	apivm.define("navigationBar", Navigationbar);

	var Ware = /*@__PURE__*/ (function(Component) {
		function Ware(props) {
			Component.call(this, props);
			this.data = {
				swiperHeight: 300,
				wareInfo: {
					name: "",
					unit: "",
					description: "",
					price: "0",
					originPrice: "0",
					stock: 0
				},

				wareCount: 0,
				recommendList: [],
				html: "",
				picList: [],
				showSharePage: false
			};
		}

		if (Component) Ware.__proto__ = Component;
		Ware.prototype = Object.create(Component && Component.prototype);
		Ware.prototype.constructor = Ware;
		Ware.prototype.apiready = function() {
			this.data.swiperHeight = api.winWidth;
			if (api.pageParam.wareCount) {
				this.data.wareCount = api.pageParam.wareCount;
			}
			// 获取商品详情数据
			this.getWareInfo(api.pageParam.wareId);
		};
		Ware.prototype.onRightButton = function() {
			this.data.showSharePage = true;
		};
		Ware.prototype.onshareresult = function(type) {
			this.data.showSharePage = false;
		};
		Ware.prototype.getWareInfo = function(wareId_) {
			var that = this;
			api.showProgress({
				title: "加载中...",
				text: ""
			});

			$kn.ajax(
				{
					url: "wares/detail",
					method: "post",
					data: {
						body: {
							id: wareId_
						}
					}
				},

				function(ret, err) {
					if (ret && ret.data) {
						var info = ret.data;
						that.data.wareInfo = info;
						if (info.picture) {
							that.data.picList = info.picture;
						}
						that.data.html = info.content
							? $kn.fitRichText(info.content, api.winWidth - 40)
							: "";
						that.fnGetRecommendWareList();
					} else {
						api.toast({
							msg: "获取商品失败"
						});
					}
					api.hideProgress();
				}
			);
		};
		Ware.prototype.fnGetRecommendWareList = function() {
			var currentCity = $kn.getCurrentCityInfo();
			var that = this;
			// 根据城市和商品分类获得相应的商品列表
			$kn.ajax(
				{
					url: "wares/recommendList",
					method: "post",
					data: {
						body: {
							supportAreaId: currentCity ? currentCity.id : "",
							wareTypeId: this.data.wareInfo.wareTypeId,
							id: this.data.wareInfo.id
						}
					}
				},

				function(ret, err) {
					if (ret && ret.data) {
						that.data.recommendList = ret.data;
					}
				}
			);
		};
		Ware.prototype.fnMinus = function() {
			if (!$kn.getUserInfo()) {
				api.openWin({
					name: "login",
					url: "../login/login.stml"
				});

				return;
			}
			if (this.data.wareCount > 0) {
				this.data.wareCount--;
				// 发送购物车更新自定义事件，传递商品Id和商品数量
				api.sendEvent({
					name: "updateShoppingCart",
					extra: {
						wareId: api.pageParam.wareId,
						wareCount: this.data.wareCount
					}
				});
			}
		};
		Ware.prototype.fnAdd = function() {
			if (!$kn.getUserInfo()) {
				api.openWin({
					name: "login",
					url: "../login/login.stml"
				});

				return;
			}
			this.data.wareCount++;
			// 发送购物车更新自定义事件，传递商品Id和商品数量
			api.sendEvent({
				name: "updateShoppingCart",
				extra: {
					wareId: api.pageParam.wareId,
					wareCount: this.data.wareCount
				}
			});
		};
		Ware.prototype.fnOpenDetailWin = function(e) {
			var id = e.currentTarget.dataset.id;
			var wareCount = e.currentTarget.dataset.wareCount;
			api.openWin({
				name: "ware" + id,
				url: "../ware/ware.stml",
				pageParam: {
					wareId: id,
					wareCount: wareCount
				},

				animation: {
					type: "fade"
				}
			});
		};
		Ware.prototype.fnOpenOrderWin = function() {
			if (!$kn.getUserInfo()) {
				api.openWin({
					name: "login",
					url: "../login/login.stml"
				});

				return;
			}
			var wareInfo = this.data.wareInfo;
			wareInfo.wareCount = this.data.wareCount > 0 ? this.data.wareCount : 1;

			var dataList = [wareInfo];
			api.openWin({
				name: "order",
				url: "../order/order.stml",
				pageParam: {
					dataList: dataList
				}
			});
		};
		Ware.prototype.render = function() {
			var this$1 = this;
			return apivm.h(
				"view",
				{class: "main"},
				apivm.h("navigationBar", {
					title: "商品详情",
					rightButtonText: "分享",
					onRightButton: this.onRightButton
				}),
				apivm.h(
					"scroll-view",
					{class: "scrollView", "scroll-y": "true"},
					apivm.h(
						"swiper",
						{
							class: "swiper",
							style: "height:" + this.data.swiperHeight + "px;",
							"indicator-dots": true,
							"indicator-active-color": "#e3007f",
							autoplay: true,
							circular: true
						},
						Object.entries(this.data.picList).map(function(ref) {
							var index$1 = ref[0];
							var _item = ref[1];
							return apivm.h(
								"swiper-item",
								null,
								apivm.h("image", {
									class: "img",
									placeholder: "../../image/default_square.png",
									src: _item,
									mode: "aspectFit",
									thumbnail: "false"
								})
							);
						})
					),
					apivm.h(
						"view",
						{class: "info"},
						apivm.h(
							"text",
							{class: "name"},
							this.data.wareInfo.name + " " + (this.data.wareInfo.unit || "")
						),
						apivm.h("text", {class: "description"}, this.data.wareInfo.description),
						apivm.h(
							"view",
							{class: "info-price"},
							apivm.h("text", {class: "price"}, "¥" + this.data.wareInfo.price),
							apivm.h(
								"text",
								{class: "origin-price"},
								"¥" + this.data.wareInfo.originPrice
							)
						),
						apivm.h(
							"text",
							{class: "stock"},
							"仅剩" + (this.data.wareInfo.stock || 0) + "件"
						)
					),
					apivm.h(
						"view",
						{class: "recommend"},
						apivm.h("text", {class: "recommendTitle"}, "推荐商品"),
						apivm.h(
							"scroll-view",
							{
								class: "recommendList",
								"scroll-x": true,
								"show-scrollbar": false,
								enhanced: true
							},
							Object.entries(this.data.recommendList).map(function(ref) {
								var index$1 = ref[0];
								var item$1 = ref[1];
								return apivm.h(
									"view",
									{
										class: "recommendItem",
										"data-id": item$1.id,
										onclick: this$1.fnOpenDetailWin
									},
									apivm.h("image", {
										class: "recommend-thumbnail",
										src: item$1.thumbnail,
										placeholder: "../../image/default_square.png"
									}),
									apivm.h(
										"text",
										{class: "recommend-info-name"},
										item$1.name + " " + (item$1.unit || "")
									),

									apivm.h("text", {class: "recommend-info-price"}, "￥" + item$1.price)
								);
							})
						)
					),
					apivm.h(
						"view",
						{class: "detail"},
						apivm.h("text", {class: "detailTitle"}, "商品详情"),
						apivm.h("rich-text", {class: "rich-text", nodes: this.data.html})
					)
				),
				apivm.h(
					"view",
					{class: "footer"},
					apivm.h("image", {
						src: "../../image/minus.png",
						class: "minus",
						onclick: function(event) {
							if (this$1.fnMinus) {
								this$1.fnMinus();
							} else {
								fnMinus();
							}
						}
					}),
					apivm.h("text", {class: "count"}, this.data.wareCount),
					apivm.h("image", {
						src: "../../image/add.png",
						class: "add",
						onclick: function(event) {
							if (this$1.fnAdd) {
								this$1.fnAdd();
							} else {
								fnAdd();
							}
						}
					}),
					apivm.h("image", {
						src: "../../image/buynow.png",
						class: "buy-button",
						onclick: this.fnOpenOrderWin
					})
				),
				apivm.h("view", {class: "minicart"}, apivm.h("minicart", null)),
				apivm.h("safe-area", {class: "safe-area"}),
				this.data.showSharePage
					? apivm.h(
							"view",
							{class: "sharepage"},
							apivm.h("sharepage", {onresult: this.onshareresult})
					  )
					: null
			);
		};

		return Ware;
	})(Component);
	apivm.define("ware", Ware);
	apivm.render(apivm.h("ware", null), "body");
})();

	window['createApp'] = function() {
		apivm.$_start();
	}
	return avm;
}